let Tools = {};

/* 检验数据类型 */
Tools.dataType = data => {

	let result = Object.prototype.toString.call(data).slice(8, -1);
	return result.toLowerCase();
}

/* 获取自定字符串里的自定内容 */
Tools.match = (regexp, string, index = -1) => {
	
	let result = string.match(regexp);

	if(typeof index == 'number' && index >= 0){
		if(!!result && result[index]){
			return result[index];
		}else{
			return false;
		}
	}
	return result;
}

/* 输出信息 */
Tools.log = (info, type = 'log') => {

	const typeLists = ['log', 'warn', 'error', 'trace', 'group', 'time', 'timeEnd', 'count'];

	if(typeLists.indexOf(type) < 0){
		console.error('你丫的需要的这种类型我这没有!!');
		return false;
	}


	switch(type){
		case 'trace':
			console.trace();
		break;
		case 'table' :
		break;
		case 'count' :
			let title = typeof info == 'string' ? info : '';
			console.count(title)
		break;

	}
}

Tools.formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(Tools.formatNumber).join('/') + ' ' + [hour, minute, second].map(Tools.formatNumber).join(':')
}

Tools.formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

Tools.differ = (d = 0, date) => {

	date = Number(date)
	var newDate = new Date(date)

	var result = '';
	var res = {};
	let year = newDate.getFullYear()
	let month = newDate.getMonth() + 1
	let day = newDate.getDate()

	let hours = newDate.getHours()
	let seconds = newDate.getSeconds()
	let minutes = newDate.getMinutes()


	if(month < 10) month = '0' + month
	if(day < 10) day = '0' + day


	result += month + '月' + day + '日';

	var week = newDate.getDay()

	var semantics = '今天'

	switch(week){
		case 0 :
		week = '星期日';
		break;
		case 1 :
		week = '星期一';
		break;
		case 2 :
		week = '星期二';
		break;
		case 3 :
		week = '星期三';
		break;
		case 4 :
		week = '星期四';
		break;
		case 5 :
		week = '星期五';
		break;
		case 6 :
		week = '星期六';
		break;
		default :
	}



	let tody = new Date();
	let tody_year = tody.getFullYear();
	let tody_month = tody.getMonth() + 1;
	let tody_day = tody.getDate();

	tody = tody_year + '/' + tody_month + '/' + tody_day;

	let tody_time = new Date(tody).getTime()

	let tody_end = tody_time + 3600 * 24 * 1000;

	if(date == tody_time){
		week = '今天';
	}else if(date == tody_end){
		week = '明天';
	}



	res.gs = year + '-' + month + '-' + day;
	res.ny = result;
	result += ' ' + week;
	res.all = result;
	res.xq = week;


	res.year = year;
	res.month = month;
	res.day = day;
	res.seconds = seconds
	res.minutes = minutes
	res.hours = hours
	

	return res

}

Tools.getUrlVars = (url) => {
    var hash;
    var myJson = {};
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        myJson[hash[0]] = hash[1];
    }
    return myJson;
}

Tools.setUrl = obj => {
	let url = [];
	
	if(Tools.dataType(obj) == 'object'){
		for(let i in obj){
			let argument = i + '=' + obj[i];
			url.push(argument)
		}

		url = url.join('&');
		return url;
	}

	return '';
}

module.exports = Tools;